/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.Serializable;
import java.util.Stack;

import org.jefb.util.JefbUtils;
import org.jefb.util.service.impl.Config;
import org.jefb.web.controller.NavAction;
import org.jefb.web.util.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component(value="directoryManagerModel")
@Scope("session")
public class DirectoryMananagerModel implements Serializable {

	// constants
	private static final long serialVersionUID = 1L;
	public static final String CURRENT_DIR_PROPERTY = "currentDir";
	public static final String SELECTED_FILE_PROPERTY = "selectedFile";
	
	private boolean showHidden=false;

	private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);
	private Stack<String> backDirectories = new Stack<String>();
	private Stack<String> forwardDirectories = new Stack<String>();

	private String currentDir = JefbUtils.FILE_SEPARATOR;
	private String workspace = JefbUtils.FILE_SEPARATOR;
	
	private FileDataHolder selectedFile;

	private File fileToCopy;

	@Autowired
	private Config config;

	public void resetModel() {
		selectedFile = null;
	}
	
	public boolean getCanUnpack(){
		if(getSelectedFile()!=null){
			FileDataHolder selectedFile = getSelectedFile();
			return FileType.isAllowedZipFile(selectedFile.getFile().getName());
		}
		return false;
	}

	public String getWorkspaceDirectory() {
		return workspace + currentDir;
	}

	public void setSelectedFile(FileDataHolder selectedFile) {
		propertyChangeSupport.firePropertyChange(SELECTED_FILE_PROPERTY,
				this.selectedFile, this.selectedFile = selectedFile);
	}

	public FileDataHolder getSelectedFile() {
		return selectedFile;
	}

	public String getWorkspace() {
		return workspace;
	}

	public void setWorkspace(String workspace) {
		this.workspace = workspace;
	}

	public boolean isCurrentDirRoot() {
		return this.currentDir.equals(JefbUtils.FILE_SEPARATOR);
	}

	public boolean getForwardDirectoriesAreEmpty() {
		return forwardDirectories.size() < 1;
	}

	public boolean getBackDirectoriesAreEmpty() {
		return backDirectories.size() < 1;
	}

	public void setCurrentDir(String currentSubpath, NavAction navAction) {
		if (currentSubpath == null || currentSubpath.isEmpty()) {
			currentSubpath = JefbUtils.FILE_SEPARATOR;
		}
		push(this.currentDir.substring(0), navAction);
		propertyChangeSupport.firePropertyChange(CURRENT_DIR_PROPERTY,
				this.currentDir, this.currentDir = currentSubpath);
	}

	private void push(String path, NavAction navAction) {
		switch (navAction) {
		case FORWARD:
			if (backDirectories.size() > 50) {
				backDirectories.remove(backDirectories.size() - 1);
			}
			backDirectories.push(path);
			break;
		case BACK:
			if (forwardDirectories.size() > 50) {
				forwardDirectories.remove(forwardDirectories.size() - 1);
			}
			forwardDirectories.push(path);
			break;
		default:
			break;
		}
	}

	public String getCurrentDir() {
		return currentDir;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	public Config getConfig() {
		return config;
	}

	public void setFileToCopy(File fileToCopy) {
		this.fileToCopy = fileToCopy;
	}

	public File getFileToCopy() {
		return fileToCopy;
	}

	public void setBackDirectories(Stack<String> backDirectories) {
		this.backDirectories = backDirectories;
	}

	public Stack<String> getBackDirectories() {
		return backDirectories;
	}

	public void setForwardDirectories(Stack<String> forwardDirectories) {
		this.forwardDirectories = forwardDirectories;
	}

	public Stack<String> getForwardDirectories() {
		return forwardDirectories;
	}

	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener propertyChangeListener) {
		propertyChangeSupport.addPropertyChangeListener(propertyName,
				propertyChangeListener);
	}

	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener propertyChangeListener) {
		propertyChangeSupport.removePropertyChangeListener(propertyName,
				propertyChangeListener);
	}

	public void setShowHidden(boolean showHidden) {
		this.showHidden = showHidden;
	}

	public boolean getShowHidden() {
		return showHidden;
	}
}
